/* $RCSFile: MonitoringDB.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/DAL/MonitoringDB.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:28  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Globalization;

using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Security;



namespace AddonNice.Settings
{
	/// <summary>
	/// DatabaseDB access code for the Monitoring Module
	/// </summary>
	public class MonitoringDB : DBDataStore
	{
/*            @PortalID				int,
    @StartDate				datetime,
    @EndDate				datetime,
    @CurrentTabs			nvarchar(200),
    @IPAddress				nvarchar(200),
    @SearchData				nvarchar(200),
    @IncludePages			int,
    @IncludeRequests			int,
    @IncludeIPAddress		int,
    @IncludeUnderCRM		int,
    @IncludeCallBack		int,
    @IncludeCrawler			int
        */

		public DataTable GetMonitoringStatsEx(DateTime startDate, 
												DateTime endDate, 
												int includePages,
												int includeRequests,
												int includeIPAddress,
												int includeUnderCRM,
												int includeCallBack,
												int includeCrawler,
												string CurrentTabs,
                                                string searchData,
                                                string IPAddresses,
												int portalID)
		{
			endDate                                     =   endDate.AddDays(1);

			// Firstly get the logged in users
			SqlDataAdapter myAdapt			            =   new SqlDataAdapter("dbo.adn_GetMonitoringEntriesEx", myConnection);
			myAdapt.SelectCommand.CommandType           =   CommandType.StoredProcedure;

            AddPortalIDParameters(myAdapt.SelectCommand,portalID);

			SqlParameter  parameterStartDate	        =   new SqlParameter("@StartDate", SqlDbType.DateTime, 8);
			parameterStartDate.Value			        =   startDate;
			myAdapt.SelectCommand.Parameters.Add(parameterStartDate);

			SqlParameter  parameterEndDate		        =   new SqlParameter("@EndDate", SqlDbType.DateTime, 8);
			parameterEndDate.Value				        =   endDate;
			myAdapt.SelectCommand.Parameters.Add(parameterEndDate);

            SqlParameter  parameterCurrentTabs          =   new SqlParameter("@CurrentTabs", SqlDbType.NVarChar, 200);
			parameterCurrentTabs.Value			        =   CurrentTabs;
			myAdapt.SelectCommand.Parameters.Add(parameterCurrentTabs);

			SqlParameter  parameterSearchData	        =   new SqlParameter("@SearchData", SqlDbType.NVarChar, 200);
			parameterSearchData.Value			        =   searchData;
			myAdapt.SelectCommand.Parameters.Add(parameterSearchData);

			SqlParameter  parameterIPAddress	        =   new SqlParameter("@IPAddress", SqlDbType.NVarChar, 200);
			parameterIPAddress.Value			        =   IPAddresses;
			myAdapt.SelectCommand.Parameters.Add(parameterIPAddress);

            SqlParameter  parameterincludePages	        =   new SqlParameter("@IncludePages", SqlDbType.Int);
			parameterincludePages.Value			        =   includePages;
			myAdapt.SelectCommand.Parameters.Add(parameterincludePages);

			SqlParameter  parameterIncludeRequests      =   new SqlParameter("@IncludeRequests", SqlDbType.Int);
			parameterIncludeRequests.Value	            =   includeRequests;
			myAdapt.SelectCommand.Parameters.Add(parameterIncludeRequests);

			SqlParameter  parameterIncludeIPAddress     =   new SqlParameter("@IncludeIPAddress", SqlDbType.Int);
			parameterIncludeIPAddress.Value		        =   includeIPAddress;
			myAdapt.SelectCommand.Parameters.Add(parameterIncludeIPAddress);

			SqlParameter  parameterincludeUnderCRM	    =   new SqlParameter("@IncludeUnderCRM", SqlDbType.Int);
			parameterincludeUnderCRM.Value			    =   includeUnderCRM;
			myAdapt.SelectCommand.Parameters.Add(parameterincludeUnderCRM);
            
			SqlParameter  parameterincludeCallBack	    =   new SqlParameter("@IncludeCallBack", SqlDbType.Int);
			parameterincludeCallBack.Value			    =   includeCallBack;
			myAdapt.SelectCommand.Parameters.Add(parameterincludeCallBack);

            SqlParameter  parameterincludeCrawler	    =   new SqlParameter("@IncludeCrawler", SqlDbType.Int);
			parameterincludeCrawler.Value			    =   includeCrawler;
			myAdapt.SelectCommand.Parameters.Add(parameterincludeCrawler);
            
			DataTable MonTb                             =   new DataTable("Monitoring");
            try
            {
                myConnection.Open();
                myAdapt.Fill(MonTb);
            }
            catch (Exception ex)
            {
                if ( MonitoringTrace.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MonitoringDB GetMonitoringStats ex : {0}", ex),MonitoringTrace.Sw.Info);
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MonitoringDB GetMonitoringStats ex : {0}", ex),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
            }
			return  CheckDataTableForDates(MonTb);
		}

        public DataTable GetMonitoringStats(DateTime startDate, 
												DateTime endDate, 
												int reportType,
                                                int  LogType,
												long currentTabID,
												bool includeMonitoringPage,
												bool includeAdminUser,
												bool includePageRequests,
												bool includeLogon,
												bool includeLogoff,
												bool includeMyIPAddress,
												bool includeUnderCRM,
												bool includeCallBack,
												bool includeCrawler,
                                                string searchData,
                                                string IPAddresses,
												int portalID)
		{
			endDate                                     =   endDate.AddDays(1);

			// Firstly get the logged in users
			SqlDataAdapter myAdapt			            =   new SqlDataAdapter("dbo.adn_GetMonitoringEntries", myConnection);
			myAdapt.SelectCommand.CommandType           =   CommandType.StoredProcedure;

            AddPortalIDParameters(myAdapt.SelectCommand,portalID);

			SqlParameter  parameterStartDate	        =   new SqlParameter("@StartDate", SqlDbType.DateTime, 8);
			parameterStartDate.Value			        =   startDate;
			myAdapt.SelectCommand.Parameters.Add(parameterStartDate);

			SqlParameter  parameterEndDate		        =   new SqlParameter("@EndDate", SqlDbType.DateTime, 8);
			parameterEndDate.Value				        =   endDate;
			myAdapt.SelectCommand.Parameters.Add(parameterEndDate);

			SqlParameter  parameterReportType	        =   new SqlParameter("@ReportType", SqlDbType.Int);
			parameterReportType.Value			        =   reportType;
			myAdapt.SelectCommand.Parameters.Add(parameterReportType);

			SqlParameter  parameterLogType	            =   new SqlParameter("@LogType", SqlDbType.Int);
			parameterLogType.Value			            =   LogType;
			myAdapt.SelectCommand.Parameters.Add(parameterLogType);

            SqlParameter  parameterCurrentTabID         =   new SqlParameter("@CurrentTabID", SqlDbType.BigInt, 8);
			parameterCurrentTabID.Value			        =   currentTabID;
			myAdapt.SelectCommand.Parameters.Add(parameterCurrentTabID);

			SqlParameter  parameterIncludeMoni	        =   new SqlParameter("@IncludeMonitorPage", SqlDbType.Bit, 1);
			parameterIncludeMoni.Value			        =   includeMonitoringPage;
			myAdapt.SelectCommand.Parameters.Add(parameterIncludeMoni);

			SqlParameter  parameterIncludeAdmin         =   new SqlParameter("@IncludeAdminUser", SqlDbType.Bit, 1);
			parameterIncludeAdmin.Value			        =   includeAdminUser;
			myAdapt.SelectCommand.Parameters.Add(parameterIncludeAdmin);

			SqlParameter  parameterIncludePageRequests  =   new SqlParameter("@IncludePageRequests", SqlDbType.Bit, 1);
			parameterIncludePageRequests.Value	        =   includePageRequests;
			myAdapt.SelectCommand.Parameters.Add(parameterIncludePageRequests);

			SqlParameter  parameterIncludeLogon		    =   new SqlParameter("@IncludeLogon", SqlDbType.Bit, 1);
			parameterIncludeLogon.Value			        =   includeLogon;
			myAdapt.SelectCommand.Parameters.Add(parameterIncludeLogon);

			SqlParameter  parameterIncludeLogoff        =   new SqlParameter("@IncludeLogoff", SqlDbType.Bit, 1);
			parameterIncludeLogoff.Value		        =   includeLogoff;
			myAdapt.SelectCommand.Parameters.Add(parameterIncludeLogoff);

			SqlParameter  parameterIncludeIPAddress     =   new SqlParameter("@IncludeIPAddress", SqlDbType.Bit, 1);
			parameterIncludeIPAddress.Value		        =   includeMyIPAddress;
			myAdapt.SelectCommand.Parameters.Add(parameterIncludeIPAddress);

			SqlParameter  parameterSearchData	        =   new SqlParameter("@SearchData", SqlDbType.NVarChar, 200);
			parameterSearchData.Value			        =   searchData;
			myAdapt.SelectCommand.Parameters.Add(parameterSearchData);

			SqlParameter  parameterIPAddress	        =   new SqlParameter("@IPAddress", SqlDbType.NVarChar, 200);
			parameterIPAddress.Value			        =   IPAddresses;
			myAdapt.SelectCommand.Parameters.Add(parameterIPAddress);

			SqlParameter  parameterincludeUnderCRM	    =   new SqlParameter("@IncludeUnderCRM", SqlDbType.Bit);
			parameterincludeUnderCRM.Value			    =   includeUnderCRM;
			myAdapt.SelectCommand.Parameters.Add(parameterincludeUnderCRM);
            
			SqlParameter  parameterincludeCallBack	    =   new SqlParameter("@IncludeCallBack", SqlDbType.Bit);
			parameterincludeCallBack.Value			    =   includeCallBack;
			myAdapt.SelectCommand.Parameters.Add(parameterincludeCallBack);

            SqlParameter  parameterincludeCrawler	    =   new SqlParameter("@IncludeCrawler", SqlDbType.Bit);
			parameterincludeCrawler.Value			    =   includeCrawler;
			myAdapt.SelectCommand.Parameters.Add(parameterincludeCrawler);
            
			DataTable MonTb                             =   new DataTable("Monitoring");
            try
            {
                myConnection.Open();
                myAdapt.Fill(MonTb);
            }
            catch (Exception ex)
            {
                if ( MonitoringTrace.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MonitoringDB GetMonitoringStats ex : {0}", ex),MonitoringTrace.Sw.Info);
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MonitoringDB GetMonitoringStats ex : {0}", ex),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
            }
			return  CheckDataTableForDates(MonTb);
		}

		public SqlDataReader GetReaderMonitoringStats(DateTime startDate, 
												DateTime endDate, 
												int reportType,
                                                int  LogType,
												long currentTabID,
												bool includeMonitoringPage,
												bool includeAdminUser,
												bool includePageRequests,
												bool includeLogon,
												bool includeLogoff,
												bool includeMyIPAddress,
												bool includeUnderCRM,
												bool includeCallBack,
												bool includeCrawler,
                                                string searchData,
                                                string IPAddresses,
												int portalID
                                                )
		{
			endDate                                     =   endDate.AddDays(1);

			// Firstly get the logged in users
			SqlCommand myCommand			            =   new SqlCommand("dbo.adn_GetMonitoringEntries", myConnection);
			myCommand.CommandType                       =   CommandType.StoredProcedure;

            AddPortalIDParameters(myCommand,portalID);

			SqlParameter  parameterStartDate	        =   new SqlParameter("@StartDate", SqlDbType.DateTime, 8);
			parameterStartDate.Value			        =   startDate;
			myCommand.Parameters.Add(parameterStartDate);

			SqlParameter  parameterEndDate		        =   new SqlParameter("@EndDate", SqlDbType.DateTime, 8);
			parameterEndDate.Value				        =   endDate;
			myCommand.Parameters.Add(parameterEndDate);

			SqlParameter  parameterReportType	        =   new SqlParameter("@ReportType", SqlDbType.Int);
			parameterReportType.Value			        =   reportType;
			myCommand.Parameters.Add(parameterReportType);

			SqlParameter  parameterLogType	            =   new SqlParameter("@LogType", SqlDbType.Int);
			parameterLogType.Value			            =   LogType;
			myCommand.Parameters.Add(parameterLogType);

            SqlParameter  parameterCurrentTabID         =   new SqlParameter("@CurrentTabID", SqlDbType.BigInt, 8);
			parameterCurrentTabID.Value			        =   currentTabID;
			myCommand.Parameters.Add(parameterCurrentTabID);

			SqlParameter  parameterIncludeMoni	        =   new SqlParameter("@IncludeMonitorPage", SqlDbType.Bit, 1);
			parameterIncludeMoni.Value			        =   includeMonitoringPage;
			myCommand.Parameters.Add(parameterIncludeMoni);

			SqlParameter  parameterIncludeAdmin         =   new SqlParameter("@IncludeAdminUser", SqlDbType.Bit, 1);
			parameterIncludeAdmin.Value			        =   includeAdminUser;
			myCommand.Parameters.Add(parameterIncludeAdmin);

			SqlParameter  parameterIncludePageRequests  =   new SqlParameter("@IncludePageRequests", SqlDbType.Bit, 1);
			parameterIncludePageRequests.Value	        =   includePageRequests;
			myCommand.Parameters.Add(parameterIncludePageRequests);

			SqlParameter  parameterIncludeLogon		    =   new SqlParameter("@IncludeLogon", SqlDbType.Bit, 1);
			parameterIncludeLogon.Value			        =   includeLogon;
			myCommand.Parameters.Add(parameterIncludeLogon);

			SqlParameter  parameterIncludeLogoff        =   new SqlParameter("@IncludeLogoff", SqlDbType.Bit, 1);
			parameterIncludeLogoff.Value		        =   includeLogoff;
			myCommand.Parameters.Add(parameterIncludeLogoff);

			SqlParameter  parameterIncludeIPAddress     =   new SqlParameter("@IncludeIPAddress", SqlDbType.Bit, 1);
			parameterIncludeIPAddress.Value		        =   includeMyIPAddress;
			myCommand.Parameters.Add(parameterIncludeIPAddress);

			SqlParameter  parameterSearchData	        =   new SqlParameter("@SearchData", SqlDbType.NVarChar, 200);
			parameterSearchData.Value			        =   searchData;
			myCommand.Parameters.Add(parameterSearchData);

			SqlParameter  parameterIPAddress	        =   new SqlParameter("@IPAddress", SqlDbType.NVarChar, 200);
			parameterIPAddress.Value			        =   IPAddresses;
			myCommand.Parameters.Add(parameterIPAddress);

			SqlParameter  parameterincludeUnderCRM	    =   new SqlParameter("@IncludeUnderCRM", SqlDbType.Bit);
			parameterincludeUnderCRM.Value			    =   includeUnderCRM;
			myCommand.Parameters.Add(parameterincludeUnderCRM);

			SqlParameter  parameterincludeCallBack	    =   new SqlParameter("@IncludeCallBack", SqlDbType.Bit);
			parameterincludeCallBack.Value			    =   includeCallBack;
			myCommand.Parameters.Add(parameterincludeCallBack);

            SqlParameter  parameterincludeCrawler	    =   new SqlParameter("@IncludeCrawler", SqlDbType.Bit);
			parameterincludeCrawler.Value			    =   includeCrawler;
			myCommand.Parameters.Add(parameterincludeCrawler);

            // Create and Fill the DataSet
			SqlDataReader result = null;
            try
            {
                myConnection.Open();
                result                                  =   myCommand.ExecuteReader();
            }
            catch (Exception ex)
            {
                if ( MonitoringTrace.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MonitoringDB GetMonitoringStats ex : {0}", ex),MonitoringTrace.Sw.Info);
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MonitoringDB GetMonitoringStats ex : {0}", ex),DBTraceSwitch.Sw.Info);
				if ( result != null && !result.IsClosed  )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
                throw;
            }
			return result;
		}


		public void LogEntry(MonLogType LogType,Guid userID,string userName,int typeCode,Guid CrmUserID,
            int portalID,int pageID,int ModuleID,int ItemID, Guid CrmItemID,string actionType, string userField, bool isUnderCRM,bool isCallBack)
		{
            if ( MonitoringTrace.Sw.TraceVerbose )
                Trace.WriteLine( 
                string.Format("[V]MonitoringDB LogEntry UID: {0}, typeCode: {1}, CRMID: {2}, PortalID: {3}, pgID: {4}, ModuleID: {5},  itemID: {6}, CrmItemID: {7}, action: {8}, uField: {9}, isUnderCRM: {10}, isCallBack: {11}", 
                new object[]{userID,typeCode,CrmUserID, portalID, pageID, ModuleID,ItemID,CrmItemID,actionType, userField,isUnderCRM,isCallBack}),MonitoringTrace.Sw.Info);
			// try/catch here because we never want the monitoring
			// code to stop pages being served.  Instead write an
			// error to the log file
            SqlConnection TheCnx                        =   null;
			try
			{
				// if a tab id of 0 is received, this is the home page
				// so change the number to 1
				// A page ID of -1 is sent when logging in and out
				if ( pageID == StStr.PageIDNotAssigned )
				{
					pageID                              =   1;
				}
                TheCnx                                  =   newConnection;
				SqlCommand myCommand			        =   new SqlCommand("dbo.adn_AddMonitoringEntry", TheCnx);
				myCommand.CommandType			        =   CommandType.StoredProcedure;

                SqlParameter parameterLogType	        =   new SqlParameter("@LogType", SqlDbType.Int, 4);
				parameterLogType.Value			        =   LogType;
				myCommand.Parameters.Add(parameterLogType);

                SqlParameter parameterUsername	        =   new SqlParameter("@UserID", SqlDbType.UniqueIdentifier);
				parameterUsername.Value			        =   userID;
				myCommand.Parameters.Add(parameterUsername);

				SqlParameter parameterUserName		    =   new SqlParameter("@UserName", SqlDbType.NVarChar, 100);
				parameterUserName.Value			        =   userName;
				myCommand.Parameters.Add(parameterUserName);

                SqlParameter parametertypeCode	        =   new SqlParameter("@typeCode", SqlDbType.Int, 4);
				parametertypeCode.Value			        =   typeCode;
				myCommand.Parameters.Add(parametertypeCode);

                SqlParameter parameterCrmUserId	        =   new SqlParameter("@CrmUserID", SqlDbType.UniqueIdentifier);
				parameterCrmUserId.Value			    =   CrmUserID;
				myCommand.Parameters.Add(parameterCrmUserId);

                AddPortalIDParameters(myCommand,portalID);

				SqlParameter parameterPageID	        =   new SqlParameter(StStr.ATSitePageID, SqlDbType.Int, 4);
				parameterPageID.Value			        =   pageID;
				myCommand.Parameters.Add(parameterPageID);

				AddModuleIDParameters(myCommand,ModuleID);

                AddItemIDParameters(myCommand,ItemID);

				SqlParameter parameterCrmItemID	        =   new SqlParameter(StStr.ATCrmItemID, SqlDbType.UniqueIdentifier);
				parameterCrmItemID.Value			    =   CrmItemID;
				myCommand.Parameters.Add(parameterCrmItemID);

                SqlParameter parameterActionType        =   new SqlParameter("@ActivityType", SqlDbType.NVarChar, 50);
				parameterActionType.Value		        =   actionType;
				myCommand.Parameters.Add(parameterActionType);

				SqlParameter parameterUserField		    =   new SqlParameter("@UserField", SqlDbType.NVarChar, 500);
				parameterUserField.Value			    =   userField;
				myCommand.Parameters.Add(parameterUserField);

				// Create the web parameters and set them to defaults.
				// If we are in the context of a web request then
				// record the extra information we can get
				SqlParameter parameterUrlReferrer	    =   new SqlParameter("@Referrer", SqlDbType.NVarChar, 255);
				parameterUrlReferrer.Value			    =   string.Empty;
				myCommand.Parameters.Add(parameterUrlReferrer);

				SqlParameter parameterUserAgent		    =   new SqlParameter("@UserAgent", SqlDbType.NVarChar, 100);
				parameterUserAgent.Value			    =   string.Empty;
				myCommand.Parameters.Add(parameterUserAgent);

				SqlParameter parameterUserHostAddress	=   new SqlParameter("@UserHostAddress", SqlDbType.NVarChar, 15);
				parameterUserHostAddress.Value			=   string.Empty;
				myCommand.Parameters.Add(parameterUserHostAddress);

				SqlParameter parameterBrowserType		=   new SqlParameter("@BrowserType", SqlDbType.NVarChar, 100);
				parameterBrowserType.Value				=   string.Empty;
				myCommand.Parameters.Add(parameterBrowserType);

				SqlParameter parameterBrowserName		=   new SqlParameter("@BrowserName", SqlDbType.NVarChar, 100);
				parameterBrowserName.Value				=   string.Empty;
				myCommand.Parameters.Add(parameterBrowserName);

				SqlParameter parameterBrowserVersion	=   new SqlParameter("@BrowserVersion", SqlDbType.NVarChar, 100);
				parameterBrowserVersion.Value			=   string.Empty;
				myCommand.Parameters.Add(parameterBrowserVersion);

				SqlParameter parameterBrowserPlatform	=   new SqlParameter("@BrowserPlatform", SqlDbType.NVarChar, 100);
				parameterBrowserPlatform.Value			=   string.Empty;
				myCommand.Parameters.Add(parameterBrowserPlatform);

				SqlParameter parameterIsUnderCRM		=   new SqlParameter("@IsUnderCRM", SqlDbType.Bit, 1);
				parameterIsUnderCRM.Value				=   isUnderCRM;
				myCommand.Parameters.Add(parameterIsUnderCRM);

				SqlParameter parameterIsCrawler		    =   new SqlParameter("@IsCrawler", SqlDbType.Bit, 1);
				parameterIsCrawler.Value				=   false;
				myCommand.Parameters.Add(parameterIsCrawler);

				SqlParameter parameterisCallBack		=   new SqlParameter("@isCallBack", SqlDbType.Bit, 1);
				parameterisCallBack.Value				=   isCallBack;
				myCommand.Parameters.Add(parameterisCallBack);
				
                // Add the browser info if we have access
				if (HttpContext.Current != null && HttpContext.Current.Request != null)
				{
					if (HttpContext.Current.Request.UrlReferrer != null)
					{
						parameterUrlReferrer.Value		=   HttpContext.Current.Request.UrlReferrer.ToString();
					}
					// Some browsers are not sending a UserAgent header
					if (HttpContext.Current.Request.UserAgent != null)
					{
						parameterUserAgent.Value		=   HttpContext.Current.Request.UserAgent;
					}
					
					parameterUserHostAddress.Value	    =   HttpContext.Current.Request.UserHostAddress;
					parameterBrowserType.Value		    =   HttpContext.Current.Request.Browser.Type;
					parameterBrowserName.Value		    =   HttpContext.Current.Request.Browser.Browser;
					parameterBrowserVersion.Value	    =   HttpContext.Current.Request.Browser.Version;
					parameterBrowserPlatform.Value	    =   HttpContext.Current.Request.Browser.Platform;
                    parameterIsCrawler.Value            =   HttpContext.Current.Request.Browser.Crawler;
				}

				// Open the database connection and execute SQL Command
				TheCnx.Open();
				myCommand.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
                if ( MonitoringTrace.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MonitoringDB LogEntry portalID: {0}, ex: {1}",portalID,ex));
                if ( DBTraceSwitch.Sw.TraceError )
    				Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MonitoringDB LogEntry portalID: {0}, ex: {1}",portalID,ex),DBTraceSwitch.Sw.Info);
			}
			finally
			{
				if ( ( TheCnx != null ) && ( TheCnx.State == System.Data.ConnectionState.Open ) )
					TheCnx.Close();
			}
		}
	}
}



